package com.web.jfinal;

import java.io.BufferedReader;
import java.io.IOException;




import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.core.Controller;

/**
 * 功能：项目对jfinal框架Controller类的扩展
 * 			1、提供getJsonPara()方法从请求中获取json数据
 * @author chenxm
 *
 */
public class JFinalCustomController extends Controller {
	private final Log log = LogFactory.getLog(JFinalCustomController.class);
	
	/**
	 * 功能：从http请求中获取json数据
	 * @return JSONObject json对象
	 */
	public JSONObject getJsonPara() {
		String json = readJSONString();
		 return JSON.parseObject(json);
	}
	
	/**
	 * 功能：从请求request中获取数据
	 * @return String 数据字符串
	 */
	private String readJSONString() {
		StringBuffer json = new StringBuffer();
		String line = null;
		
		try {
			BufferedReader br = new BufferedReader(getRequest().getReader());
			while((line = br.readLine()) != null) {
				json.append(line);
			}
		} catch (IOException e) {
			log.error("从http request中获取流数据出错");
			e.printStackTrace();
		}
		
		return json.toString();
	}
}
